package com.ww.dao.impl;


import com.ww.dao.NewsDao;
import com.ww.entity.News;
import com.ww.util.DBUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class NewsDaoImpl implements NewsDao {
    private Connection conn = DBUtil.getConnection();
    private QueryRunner qr = new QueryRunner();
    @Override
    public int save(News obj) {
        String sql = "INSERT INTO  tb_news(news_id,news_title,news_content,news_time,news_nums,news_img,news_type) VALUES (?,?,?,?,?,?,?)";
        Object[] paras = {obj.getNews_id(),obj.getNews_title(),obj.getNews_content(),obj.getNews_time(),obj.getNews_nums()
        ,obj.getNews_img(),obj.getNews_type()};
        try {
            int row = qr.update(conn, sql, paras);
            return row;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int update(News obj) {
        String sql = "UPDATE  tb_news SET  news_title=?,news_content=?,news_time=?,news_nums=?,news_img=?,news_type=? WHERE news_id=?  ";
        Object[] paras = {obj.getNews_title(),obj.getNews_content(),obj.getNews_time(),obj.getNews_nums()
                ,obj.getNews_img(),obj.getNews_type(),obj.getNews_id()};
        try {
            int row = qr.update(conn, sql, paras);
            return row;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int delete(Object id) {
        String sql = "DELETE FROM tb_news WHERE news_id  = ? ";
        Object[] paras = {id};
        try {
            int row = qr.update(conn, sql, paras);
            return row;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public News findById(Object id) {
        String sql = "SELECT * FROM tb_news where news_id  = ?";
        Object[] paras = {id};
        try {
            News news = qr.query(conn, sql, paras, new BeanHandler<News>(News.class));
            return news;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<News> findAll() {
        String sql = "SELECT * FROM tb_news";
        Object[] paras = {};
        try {
            List<News> newsList = qr.query(conn, sql, paras, new BeanListHandler<News>(News.class));
            return newsList;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<News> findSearch(String keyword) {
        String sql = "SELECT * FROM tb_news where news_title like concat('%',concat(?,'%'))";
        Object[] paras = {keyword};
        try {
            List<News> newsList = qr.query(conn, sql, paras, new BeanListHandler<News>(News.class));
            return newsList;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<News> findPage(int page, int pagesize) {
        String sql = "SELECT * FROM tb_news LIMIT ?,?";
        Object[] paras = {(page-1)*pagesize,pagesize};
        try {
            List<News> newsList = qr.query(conn, sql, paras, new BeanListHandler<News>(News.class));
            return newsList;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public long count() {
        String sql = "select count(*) from tb_news";
        Object[] paras = {};
        try {
            long nums = qr.query(conn, sql, paras, new ScalarHandler<Long>());
            return nums;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public List<News> findnums() {
        String sql = "SELECT * FROM tb_news ORDER BY news_nums DESC";
        Object[] paras = {};
        try {
            List<News> newsList = qr.query(conn, sql, paras, new BeanListHandler<News>(News.class));
            return newsList;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int increaseReadNums(String id) {
        String sql="update tb_news set news_nums=news_nums+1 WHERE news_id=?";
        Object[] paras={id};
        try {
            int row = qr.update(conn, sql, paras);
            return row;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
}
